<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Users;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Users\Model\User;
use Users\Model\UserTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;


use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),   
        );
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
	    
		// SERVICES
		'AuthService' => function($sm) {
		    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
		    $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user1','email','password', 'MD5(?)');
			    
		    $authService = new AuthenticationService();
		    $authService->setAdapter($dbTableAuthAdapter);
		    return $authService;
		},
				
                'Users\Model\UserTable' => function($sm){
                    $tableGateWay = $sm->get('UserTableGateway');
                    $table = new UserTable($tableGateWay); 
                    return $table;
                } ,
                'UserTableGateway' => function($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new User());
                    return new TableGateway('user', $dbAdapter , null , $resultSetPrototype);
                } ,
                'Users\Model\GroupChatTable' => function($sm){
                    $tableGateway = $sm->get('GroupChatGateway');
                    return new \Users\Model\GroupChatTable($tableGateway);
                },        
                'GroupChatGateway' => function($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new \Users\Model\GroupChat());
                    return new TableGateway('chat_messages' , $dbAdapter , null , $resultSetPrototype);
                },
                'ChatMessagesTableGateway' => function($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new TableGateway('chat_messages' , $dbAdapter);
                },
                /*----------------------FORM---*/
                'loginform' => function($sm){
                    $form = new \Users\Form\LoginForm();
                    return $form;
                },
                'UserEditForm' => function($sm){
                    $form = new \Users\Form\UserEditForm();
                    return $form;
                },
                'GroupChatForm' => function($sm){
                    $form = new \Users\Form\GroupChatForm();
                    return $form;
                },
                'formSearch' => function($sm){
                    $form = new \Users\Form\SearchForm();
                    return $form;
                },
                        
                'AuthService' => function($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $dbTableAdapter = new DbTableAuthAdapter($dbAdapter , 'user' , 'email' , 'password' , 'MD5(?)');
                    $authservice = new AuthenticationService();
                    $authservice->setAdapter($dbTableAdapter);
                    return $authservice;
                },
	
            ),
        );
    }
}
